namespace StarForce.Manager
{
    public partial class EliminateInspectManager
    {
        
        /// <summary>
        /// 方块填充
        /// </summary>
        public class EliminateFill
        {

            private EliminateInspectManager m_EliminateInspectManager;

            public EliminateFill(EliminateInspectManager eliminateInspectManager)
            {
                m_EliminateInspectManager = eliminateInspectManager;
            }

            public void AllFill()
            {
                while (Fill())  
                {
                    
                }        
                m_EliminateInspectManager.CheckAll();
            }
            
            /**
             * 1. 从倒数第二排开始 从下往上开始遍历 如果该方块下面得方块为空 那么就往下移动
             */

            public bool Fill()
            {
                bool fillFinish = false;

                //从倒数第二排开始遍历 -行遍历
                for (int i = Constant.EliminateGameConfig.HCount-2; i >= 0; i--)
                {
                    for (int j = 0; j < Constant.EliminateGameConfig.VCount; j++)
                    {
                        EliminateItem item = GameEntry.Eliminate.GetItem(i + (j*Constant.EliminateGameConfig.VCount));

                        if (item !=null)
                        {
                            if (item.BItem == null)
                            {
                                item.MoveTo((i + (j*Constant.EliminateGameConfig.VCount))+1);
                                fillFinish = true;
                            }
                        } 
                    }
                }
                
                //最上排情况

                for (int i = 0; i < Constant.EliminateGameConfig.VCount; i++)
                {
                    EliminateItem item = GameEntry.Eliminate.GetItem(i * Constant.EliminateGameConfig.VCount);

                    if (item == null)
                    {
                        GameEntry.Eliminate.Show( EliminateGameUtility.GetRandomData(i * Constant.EliminateGameConfig.VCount,true));
                        fillFinish = true;
                    }
                }

                return fillFinish;
            }
        }
        
    }
}